﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace KickStart.Helpers
{
    public static class HtmlHelpersExtensions
    {
        #region Show Highlight

        private static void showHighlight(HtmlHelper html, string message)
        {
            html.RenderPartial("_highlight", new ViewDataDictionary { { "errormessage", message } });
        }

        public static void ShowHighlight<T>(this HtmlHelper<T> html, string message) where T : class
        {
            showHighlight(html, message);
        }
        public static void ShowHighlight(this HtmlHelper html, string message)
        {
            showHighlight(html, message);
        }

        #endregion

        #region Show Error

        public static void ShowError(this HtmlHelper html, string message)
        {
            showError(html, message);
        }

        public static void ShowError<T>(this HtmlHelper<T> html, string message) where T : class
        {
            showError(html, message);
        }

        private static void showError(HtmlHelper html, string message)
        {
            html.RenderPartial("_error", new ViewDataDictionary { { "errormessage", message } });
        }

        #endregion

        #region Show Question

        public static void ShowQuestion(this HtmlHelper html, string message)
        {
            showQuestion(html, message);
        }

        public static void ShowQuestion<T>(this HtmlHelper<T> html, string message) where T : class
        {
            showQuestion(html, message);
        }

        private static void showQuestion(HtmlHelper html, string message)
        {
            html.RenderPartial("_question", new ViewDataDictionary { { "errormessage", message } });
        }

        #endregion

        public static void RenderError(this HtmlHelper html, string message)
        {
            html.ViewData["errormessage"] = message;
            html.RenderPartial("_error");
        }

        public static void RenderHighlight(this HtmlHelper html, string message)
        {
            html.ViewData["errormessage"] = message;
            html.RenderPartial("_highlight");
        }

        public static string DeleteLink(this AjaxHelper ajax, string linkText,
                                        string actionName, string controllerName,
                                        string modelId, string confirmText,
                                        string elementToHide)
        {
            string temp = TemplateHelpers.Get("_link_delete");
            return temp
                .Replace("[link_text]", linkText)
                .Replace("[confirm]", confirmText)
                .Replace("[url]", string.Format("/{0}/{1}", controllerName, actionName))
                .Replace("[id]", modelId)
                .Replace("[deleted]", string.IsNullOrEmpty(elementToHide) ? Guid.NewGuid().ToString() : elementToHide);

        }


        public static string SubmitLink(this AjaxHelper ajax, string linkText, string icon)
        {
            string temp = string.IsNullOrEmpty(icon) ? TemplateHelpers.Get("_link_submit_no_icon") : TemplateHelpers.Get("_link_submit");
            return temp.Replace("[link_text]", linkText).Replace("[icon]", icon);
        }

        public static string Link(this HtmlHelper html, string linkText, string url)
        {
            return Link(html, linkText, url, "");
        }

        public static string Link(this HtmlHelper html, string linkText, string url, string icon)
        {
            string temp = string.IsNullOrEmpty(icon) ? TemplateHelpers.Get("_link_no_icon") : TemplateHelpers.Get("_link");
            return temp.Replace("[link_text]", linkText).Replace("[icon]", icon).Replace("[link_url]", url);
        }



        public static string FormTextBox(this HtmlHelper html, string name, string label)
        {
            //var pTag = new TagBuilder("p");
            //var labelTag = new TagBuilder("label");
            //labelTag.MergeAttribute("for", name);
            //labelTag.SetInnerText(label);
            //pTag.InnerHtml = labelTag.ToString() + " " + html.TextBox(name);
            //return pTag.ToString();
            string temp = TemplateHelpers.Get("_form_textbox");
            return temp
                .Replace("[name]", name)
                .Replace("[label]", label);
        }

        public static string FormPassword(this HtmlHelper html, string name, string label)
        {
            //var pTag = new TagBuilder("p");
            //var labelTag = new TagBuilder("label");
            //labelTag.MergeAttribute("for", name);
            //labelTag.SetInnerText(label);
            //pTag.InnerHtml = labelTag.ToString() + " " + html.Password(name);
            //return pTag.ToString();
            string temp = TemplateHelpers.Get("_form_password");
            return temp
                .Replace("[name]", name)
                .Replace("[label]", label);
        }

        public static string FormCheckBox(this HtmlHelper html, string name, string label)
        {
            //var pTag = new TagBuilder("p");
            //var labelTag = new TagBuilder("label");
            //labelTag.MergeAttribute("for", name);
            ////labelTag.MergeAttribute("class", "inline");
            //labelTag.SetInnerText(label);
            //pTag.InnerHtml = html.CheckBox(name) + labelTag.ToString();
            //return pTag.ToString();

            string temp = TemplateHelpers.Get("_form_checkbox");
            return temp
                .Replace("[name]", name)
                .Replace("[label]", label);
        }

    }

    public static class TemplateHelpers
    {
        private static readonly Dictionary<string, string> cache;
        private static readonly string path;

        //todo - enable cache to improve performance

        private static bool enableCache = false;

        static TemplateHelpers()
        {
            cache = new Dictionary<string, string>();
            path = HttpContext.Current.Server.MapPath("~/templates");
        }

        public static string Get(string name)
        {
            if (!enableCache || !cache.ContainsKey(name))
                cache[name] = System.IO.File.ReadAllText(string.Format("{0}/{1}.htm", path, name));

            return cache[name];
        }
    }

}